 
 
import { StationNetwork } from "GameLoop/Path/StationNetwork";
import { MineEntity } from "./MineEntity";
import { PositionHelper } from "utils/position/PositionHelper";
import { UtilsHelper } from "utils/position/UtilsHelper";

export class InnerMineEntity<T extends IInnerMineMemory =IInnerMineMemory> extends MineEntity<T>  implements ILinkModule
{
    
    
    public initAttributes(): void 
    {
        super.initAttributes();
        const nodememory = this.getNodeMemory();
     
        if(!nodememory.max)
        {
            nodememory.max = Math.min(this.BasePosition.getFreeSpace(1).length,3);
        }

        if(!nodememory.skey)
        {
            nodememory.skey = UtilsHelper.serializeObject(this.BasePosition.lookFor(LOOK_SOURCES)[0]);
        }
        this._source = UtilsHelper.deSerializeObject(nodememory.skey)

        this.initPath(nodememory);
        this._pickupPos = this.BasePosition.getFreeSpace(1);
       
    }
    public updateMemory(): void 
    {
        // 更新container 和link
        if(this._initializer.plan)
        {
            for(let k in this._initializer.plan.structures)
            {
                const pos = this._initializer.plan.structures[k as StructureConstant].map(e=>{return {roomName:this.roomName,x:e.x,y:e.y}})
                const ids = this.getStructures(k as StructureConstant,pos);
                switch(k )
                {
                    case STRUCTURE_CONTAINER:
                        this.getNodeMemory().containerId = ids[0] as Id<StructureContainer>;
                        break;
                    case STRUCTURE_LINK:
                        this.getNodeMemory().linkId = ids[0] as Id<StructureLink>;
                        break;
                }
            }
        }
    
        // 更新site
        if(!this.getNodeMemory().sites || this.getNodeMemory().sites?.length ==0)
        {
            // 没有site
            const sites = this.getSource().pos.lookForInRange(LOOK_CONSTRUCTION_SITES,3);
            if(sites && sites.length>0)
            {
                this.getNodeMemory().sites = sites.map(e=>e.constructionSite.id);
            }
            else
            {
                delete this.getNodeMemory().sites;
            }
        }
        if(this.Link)
        {
            this.getContainer()?.destroy();
            this.getNodeMemory().containerId = undefined;
        }
        else
        {
            
            // 更新维修目标
            const container = this.getContainer() ;
            if(container && container.hits<container.hitsMax*0.96)
            {
                this.getNodeMemory().repairObjects = [container.id];
            }
        }

    }

    protected get plan()
    {
        return this._initializer.plan as IModulePlan;
    }

    public get Link()
    {
        const links= this.getStructuresByRelative(STRUCTURE_LINK,this.plan.structures[STRUCTURE_LINK],PositionHelper.Create({roomName:this.roomName,x:0,y:0}) );
        return Game.getObjectById(links[0]) as StructureLink | undefined;
    }

    public get Extensions()
    {
        const extensions= this.getStructuresByRelative(STRUCTURE_EXTENSION,this.plan.structures[STRUCTURE_EXTENSION],PositionHelper.Create({roomName:this.roomName,x:0,y:0}) );
        return extensions.map(e=> Game.getObjectById(e));
    }
    private get ExtensionIds()
    {
        const extensions= this.getStructuresByRelative(STRUCTURE_EXTENSION,this.plan.structures[STRUCTURE_EXTENSION],PositionHelper.Create({roomName:this.roomName,x:0,y:0}) );
        return extensions;
    }
    public  getTransferObject(): AnyStoreStructure |null
    {
        for(const e of this.ExtensionIds)
        {
            const ext = Game.getObjectById(e);
            if(ext && ext.store.getFreeCapacity(RESOURCE_ENERGY)>0)
            {
                return ext;
            }
        }
        const link = this.Link;
        if(link && link.store.getFreeCapacity(RESOURCE_ENERGY)>0)
        {
            return link;
        }
        return null
    }
    public getCarryBodyNum(CarrySpeed:number=1):number
    {
        if(this.getNodeMemory().linkId)
        {
            return 0;
        }
        return super.getCarryBodyNum(CarrySpeed);
        
    }
    // 每Tick采集的能量数量
    public getEnergyHarvestSpeed():number
    {
        return  10* this.getHarvestEfficiency(3000);
    }
    public getWorkBodyNum():number
    {
        return 6* this.getHarvestEfficiency(3000);
    }

    
}
